package com.hnkywl.sts.action.xsgl;

import com.hnkywl.sts.dao.xsgl.XsmxDao;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.htgl.CghtService;
import com.hnkywl.sts.service.htgl.XshtService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.xsgl.XsmxService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.bsf.util.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.StringUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Melody_for_love
 * Date: 13-7-16
 * Time: 下午3:32
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/xsgl/xsmx")
public class XsmxAction extends SimpleActionSupport {
    private XsmxService xsmxService;
    private XshtService xshtService;
    //销售单号
    private String xsdh;
    //申报公司ID
    private Long sbgsId;
    //发货日期
    private Date fhrqStart;
    private Date fhrqEnd;
    private Date currentTime = new Date();
    //状态
    private String zt;
    private String jszt;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    private int pageNo;
    private int pageSize;

    private List<Gncz> gnczList;
    private GnczService gnczService;
    private String gcmc;
    private XsmxDao xsmxDao;
    private Long gcId;
    private Long gysId;
    private String cght;
    private String htbm;
    private String xhtbm;
    private Long khId;
    private Long cghtId;
    private Long xshtId;
    private String kclx;
    private String xszxh;
    private String carNo;
    private String bindNo;


    @Action("xsmx")
    public String xsmx() {
        currentTime = new Date();
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //销售明细的查询分页显示
    @Action("xsmxList")
    public void xsmxList() {
        this.renderJson(this.xshtService.findAllXsHtTj(htbm, xhtbm, khId, fhrqStart, fhrqEnd, zt, page, rows));
    }

    //销售明细的查询分页显示
    @Action("shmxList")
    public void shmxList() {
        this.renderJson(this.xsmxService.shmxList(sbgsId, gysId, khId, cght, xsdh, fhrqStart, fhrqEnd, page, rows));
    }

    // 采购明细
    @Action("cghtgldetail")
    public String cghtgldetail() {
        return SUCCESS;
    }


    @Action("sskcdetail")
    public String sskcdetail() {
        return SUCCESS;
    }


    // 库存信息
    @Action("sskcDetailList")
    public void sskcDetailList() {
        this.renderJson(this.xsmxService.findSskcList(xszxh, carNo, bindNo, kclx, xshtId, 1, 99999999));
    }

    // 销售信息
    @Action("xsmxdetail")
    public String xsmxdetail() {
        return SUCCESS;
    }

    @Action("xsmxDetailList")
    public void xsmxDetailList() {
        this.renderJson(this.xsmxService.findXsmxJfmx(xsdh, xszxh, carNo, bindNo, xshtId, 1, 99999999));
    }

    /**
     * 销售明细的导出
     *
     * @return
     */
    @Action("xsmx_export")
    public String xsmxExport() {
      /*  Map<String, List> map = this.xsmxDao.exportxsmx(xsdh, xslx, djlx, sbgsId, sbbmId, khId, ggName, ggCode, ggXh, fhrqStart, fhrqEnd, zt, jszt, gcId, ckId);
        List<Map> xsmxList = map.get("result");
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        sheet.setColumnWidth((short) 0, (short) (3 * 512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        //生成第一行数据
        poiUtil.fillRow(sheet, headerStyle, 0, "序号", "销售单号", "制单日期", "出库日期", "新物资编号", "老物资编号", "品名", "规格", "单位", "图号",
                "应发数量", "出售总金额(元)", "实发数量", "结算单价(元)", "结算总金额(元)", "税额(元)", "出库数量", "库存单价(元)", "库存总金额(元)",
                "客户", "工程名称", "发票号码", "供应商", "产地", "提货方式", "公司名称", "部门名称");
        //表体部分
        for (int i = 1; i < xsmxList.size() + 1; i++) {
            Map item = xsmxList.get(i - 1);
            poiUtil.fillRow(sheet, null, i, item.get("xh"), item.get("xsdh"), item.get("dateCreated"), item.get("cksj"), item.get("wzggcode"), item.get("gg.oldcode"),
                    item.get("ggName"), item.get("wzggggxh"), item.get("dw"), item.get("th"), item.get("sbsl2"),
                    item.get("csje"), item.get("sbsl1"), item.get("bhsj"), item.get("jsje"), item.get("se"),
                    item.get("cksbsl"), item.get("kcdj"), item.get("kcje"), item.get("khJc"), item.get("gcmc"),
                    item.get("fphm"), item.get("gysJc"), item.get("cd"), item.get("fyzr"), item.get("sbgsJc"), item.get("sbbmJc"));
        }
        List<Map> sumItem = map.get("sum");
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRow(sheet, sumStyle, xsmxList.size() + 1, "", "合计：", "", "", "", "", "", "", "",
                sumItem.get(0).get("sbsl"), sumItem.get(0).get("csje"), "", "", sumItem.get(0).get("jsje"), sumItem.get(0).get("hse"), sumItem.get(0).get("ck_sbsl"), "",
                sumItem.get(0).get("kcje"), "", "", "", "", "", "", "", "");
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("销售明细汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }*/
        return null;
    }

    @Action("xsmxdetail_xsmx_exp")
    public String xsmxExp() {
        List<Map> htmx = xsmxService.findXsmxExpList(xsdh, xszxh, carNo, bindNo, xshtId, 1, 99999999);
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 15);
        HSSFCellStyle titleStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, titleStyle, 0, "销售单号", "制单日期", "合同子项号", "品名", "材质", "规格", "车皮号", "捆包号", "单位",
                "入库日期", "出库日期", "件数", "入库数量(吨)", "剩余数量(吨)", "合同单价(含税)", "实际结算价", "购货金额", "代理费", "贴息", "资金占用费", "赎货金额", "赎货时间", "关联收款 ");
        //i=1,get(i-1)，打印才有列名
        for (int i = 1; i < htmx.size() + 1; i++) {
            Map item = htmx.get(i - 1);
            Double shje = 0.0;
            Double ghje = 0.0;
            Double txje = 0.0;
            Double dlfje = 0.0;
            Double zjzyf = 0.0;
            if (null != item.get("ghje") && org.apache.commons.lang3.StringUtils.isNotBlank(item.get("ghje").toString())) {
                ghje = Double.valueOf(item.get("ghje").toString());
            }
            if (null != item.get("txje") && org.apache.commons.lang3.StringUtils.isNotBlank(item.get("txje").toString())) {
                txje = Double.valueOf(item.get("txje").toString());
            }
            if (null != item.get("dlfje") && org.apache.commons.lang3.StringUtils.isNotBlank(item.get("dlfje").toString())) {
                dlfje = Double.valueOf(item.get("dlfje").toString());
            }
            if (null != item.get("zjzyf") && org.apache.commons.lang3.StringUtils.isNotBlank(item.get("zjzyf").toString())) {
                zjzyf = Double.valueOf(item.get("zjzyf").toString());
            }
            shje = ghje + dlfje + txje+zjzyf;
            poiUtil.fillRow(sheet, null, i, item.get("xsdh"), item.get("zdrq"), item.get("xsxt_xsdh"), item.get("name"),
                    item.get("cz"), item.get("ggxh"), item.get("carno"), item.get("bindno"), item.get("dw"), item.get("rkrq"), item.get("ckrq"),
                    item.get("rkjs"), item.get("rksl"), item.get("sbsl"), item.get("hsj"), item.get("sjjsj"), item.get("ghje"), item.get("dlfje"), item.get("txje"), item.get("zjzyf"), shje, item.get("shsj"), item.get("bm"));
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("销售明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            OutputStream out = response.getOutputStream();
            wb.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }
    //==========getter and setter============


    public Long getGcId() {
        return gcId;
    }

    public void setGcId(Long gcId) {
        this.gcId = gcId;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getGcmc() {
        return gcmc;
    }

    public void setGcmc(String gcmc) {
        this.gcmc = gcmc;
    }

    public Date getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }


    public String getXsdh() {
        return xsdh;
    }

    public void setXsdh(String xsdh) {
        this.xsdh = xsdh;
    }


    public Date getFhrqStart() {
        return fhrqStart;
    }

    public void setFhrqStart(Date fhrqStart) {
        this.fhrqStart = fhrqStart;
    }

    public Date getFhrqEnd() {
        return fhrqEnd;
    }

    public void setFhrqEnd(Date fhrqEnd) {
        this.fhrqEnd = fhrqEnd;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public String getJszt() {
        return jszt;
    }

    public void setJszt(String jszt) {
        this.jszt = jszt;
    }

    public Long getGysId() {
        return gysId;
    }

    public void setGysId(Long gysId) {
        this.gysId = gysId;
    }

    public String getCght() {
        return cght;
    }

    public void setCght(String cght) {
        this.cght = cght;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    public String getXhtbm() {
        return xhtbm;
    }

    public void setXhtbm(String xhtbm) {
        this.xhtbm = xhtbm;
    }

    public Long getCghtId() {
        return cghtId;
    }

    public void setCghtId(Long cghtId) {
        this.cghtId = cghtId;
    }

    public Long getXshtId() {
        return xshtId;
    }

    public void setXshtId(Long xshtId) {
        this.xshtId = xshtId;
    }

    public String getKclx() {
        return kclx;
    }

    public void setKclx(String kclx) {
        this.kclx = kclx;
    }

    public String getXszxh() {
        return xszxh;
    }

    public void setXszxh(String xszxh) {
        this.xszxh = xszxh;
    }

    public String getCarNo() {
        return carNo;
    }

    public void setCarNo(String carNo) {
        this.carNo = carNo;
    }

    public String getBindNo() {
        return bindNo;
    }

    public void setBindNo(String bindNo) {
        this.bindNo = bindNo;
    }

    @Autowired
    public void setXsmxService(XsmxService xsmxService) {
        this.xsmxService = xsmxService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setXsmxDao(XsmxDao xsmxDao) {
        this.xsmxDao = xsmxDao;
    }

    @Autowired
    public void setXshtService(XshtService xshtService) {
        this.xshtService = xshtService;
    }

}
